package com.xinchao.api;

import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.AdvertisementMonitorChargeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * 费用
 *
 * @author dxy
 * @date 2018/11/13 9:36
 */
@Controller
@RequestMapping(value = "/charge")
public class AdvertisementMonitorChargeController {
	private static Logger logger = LoggerFactory.getLogger(AdvertisementMonitorChargeController.class);
	@Autowired
	private AdvertisementMonitorChargeService advertisementMonitorChargeService;

	/**
	 * 获取财务-监播-消费情况列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listCharge")
	public Object listCharge(String advertisementName, Integer timeType) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//时间类型
		if (timeType == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "时间类型不能为空");
		}
		try {
			return advertisementMonitorChargeService.listChargeVO(advertisementName, timeType);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorChargeController listCharge 获取财务-监播-消费情况列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorChargeController listCharge 获取财务-监播-消费情况列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}


	/**
	 * 获取财务-监播-时段分析消费列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listTimeFrameCharge")
	public Object listTimeFrameCharge(String advertisementName, String date) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//日期
		if (StringUtils.isBlank(date)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "日期不能为空");
		}
		try {
			return advertisementMonitorChargeService.listTimeFrameChargeVO(advertisementName, date);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorChargeController listTimeFrameCharge 获取财务-监播-时段分析消费列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorChargeController listTimeFrameCharge 获取财务-监播-时段分析消费列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 获取用户余额
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserBalance")
	public Object getUserBalance(String userName) {
		//用户名称
		if (StringUtils.isBlank(userName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "用户名称不能为空");
		}
		try {
			Map<String, Object> balanceMap = new HashMap<>();
			Double userBalance = advertisementMonitorChargeService.getUserBalance(userName);
			balanceMap.put("balance", userBalance);
			balanceMap.put("userName", userName);
			return balanceMap;
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorChargeController getUserBalance 获取用户余额失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		}
	}

}
